﻿namespace Logger7;

public static class LoggerExtensions
{
    private static readonly Func<SqlServerState, Exception?, string> _messageFormatter = MessageFormatter;

    public static long LogError(this ILogger logger, Exception ex, string msg, string? userExt = null)
    {
        var state = new SqlServerState { Message = msg, UserExt = userExt };
        logger.Log(LogLevel.Error, new EventId(), state, ex, _messageFormatter);
        return state.LoggerId;
    }
    public static long LogWarning(this ILogger logger, Exception ex, string msg, string? userExt = null)
    {
        var state = new SqlServerState { Message = msg, UserExt = userExt };
        logger.Log(LogLevel.Warning, new EventId(), state, ex, _messageFormatter);
        return state.LoggerId;
    }

    public static long LogInformation(this ILogger logger, Exception ex, string msg, string? userExt = null)
    {
        var state = new SqlServerState { Message = msg, UserExt = userExt };
        logger.Log(LogLevel.Information, new EventId(), state, ex, _messageFormatter);
        return state.LoggerId;
    }

    public static long LogError(this ILogger logger, string msg, string? userExt = null)
    {
        var state = new SqlServerState { Message = msg, UserExt = userExt };
        logger.Log(LogLevel.Error, new EventId(), state, null, _messageFormatter);
        return state.LoggerId;
    }
    public static long LogWarning(this ILogger logger, string msg, string? userExt = null)
    {
        var state = new SqlServerState { Message = msg, UserExt = userExt };
        logger.Log(LogLevel.Warning, new EventId(), state, null, _messageFormatter);
        return state.LoggerId;
    }

    public static long LogInformation(this ILogger logger, string msg, string? userExt = null)
    {
        var state = new SqlServerState { Message = msg, UserExt = userExt };
        logger.Log(LogLevel.Information, new EventId(), state, null, _messageFormatter);
        return state.LoggerId;
    }


    private static string MessageFormatter(SqlServerState state, Exception? error)
    {
        return state.Message!;
    }

}
